﻿using System.Collections.Generic;
using UnityEngine;
using Yoozoo.Managers.ResourceManagerV2.Runtime;

namespace Yoozoo.Gameplay.City
{
    public class CityGridComponent : MonoBehaviour
    {
        public List<string> maskTexturePaths;

        public MeshRenderer gridRenderer;
        private Material _material;
        [SerializeField]
        private Texture2D _maskTexture;

        private ResLoader _maskLoader;

        public void ChangeGridMask(int index)
        {
            if (!_material)
            {
                _material = gridRenderer.sharedMaterial;
            }

            if (_maskLoader == null)
            {
                _maskLoader = ResLoader.Alloc();
            }

            _maskLoader.Add2Load(maskTexturePaths[index], typeof(Texture2D), (success, assetName, asset) =>
            {
                _maskTexture = Instantiate(asset as Texture2D);
                _material.SetTexture("_MaskTex", _maskTexture);
            });
            _maskLoader.Load();
        }

        public void Dispose()
        {
            if (_maskTexture)
            {
                Destroy(_maskTexture);
                _maskTexture = null;
            }

            _material = null;

            if (_maskLoader!=null)
            {
                _maskLoader.Recycle2Cache();
                _maskLoader = null;
            }
        }
        
    }
}
